//    Openbravo POS is a point of sales application designed for touch screens.
//    Copyright (C) 2008-2009 Openbravo, S.L.
//    http://www.openbravo.com/product/pos
//
//    This file is part of Openbravo POS.
//
//    Openbravo POS is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Openbravo POS is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Openbravo POS.  If not, see <http://www.gnu.org/licenses/>.

package com.openbravo.pos.panels;

import cn.ekuma.epos.datalogic.I_DataLogicSales;

import com.openbravo.bean.AttributeValue;
import com.openbravo.bean.ProductAttributeValuePrice;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.ComboBoxValModel;
import com.openbravo.format.Formats;

import java.awt.Component;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

/**
 *
 * @author adrian
 */
public class JProductAttListItem extends javax.swing.JPanel implements I_ProductAttEdit {

    private String attid;
    private ComboBoxValModel model;
    I_DataLogicSales dlSale;
	private JLabel jdeffPrice;
    private String productId;
    ProductAttributeValuePrice valuePrice;
	
    /** Creates new form JProductAttListItem */
    public JProductAttListItem(String attid, String label, String value, List<AttributeValue> values,I_DataLogicSales dlSale,String productId) {

        this.attid = attid;
        this.dlSale=dlSale;
        this.productId=productId;
        initComponents();

        jLabel1.setText(label);

        for (AttributeValue item : values) {
            jValues.addItem(item.getValue());
        }
        jValues.setSelectedItem(value);
    }

    public void assignSelection() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                jValues.requestFocus();
            }
        });
    }

    public String getAttribute() {
        return attid;
    }

    public String getValue() {
        return (String) jValues.getSelectedItem();
    }

    public Component getComponent() {
        return this;
    }

    

	@Override
	public ProductAttributeValuePrice getProductAttValuePrice() {
		return valuePrice;
	}

	/** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jValues = new javax.swing.JComboBox();
        jValues.addItemListener(new ItemListener() {
        	public void itemStateChanged(ItemEvent e) {
        		checkProductAttributePrice((String) e.getItem());
        	}
        });

        jLabel1.setText("jLabel1");
        
        jdeffPrice = new JLabel("0.0");
        jdeffPrice.setHorizontalAlignment(SwingConstants.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        layout.setHorizontalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap()
        			.addComponent(jLabel1, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(jValues, GroupLayout.PREFERRED_SIZE, 193, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(jdeffPrice, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)
        			.addContainerGap(75, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
        	layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(layout.createSequentialGroup()
        			.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        			.addGroup(layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel1)
        				.addComponent(jValues, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(jdeffPrice)))
        );
        this.setLayout(layout);
    }// </editor-fold>//GEN-END:initComponents


    protected void checkProductAttributePrice(String value) {
    	valuePrice=null;
    	if(value==null||productId==null){
    		jdeffPrice.setText("0.0");
    	    return ;
    	}
		try {
			 valuePrice=dlSale.findProductAttributePrice(productId,value);
		} catch (BasicException e) {
			e.printStackTrace();
		}
		if(valuePrice==null)
			jdeffPrice.setText("0.0");
		else
			jdeffPrice.setText(Formats.CURRENCY.formatValue(valuePrice.getDiffprice()));
	}


	// Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JComboBox jValues;
}
